package com.congee02.bt.dfs.top2bottom;

import com.congee02.bt.TreeNode;

public class SumEvenGrandparent {

    public int sumEvenGrandparent(TreeNode root) {

        return process(root, false, false);

    }

    private int process(TreeNode root, boolean fatherEven, boolean addable) {
        if (root == null) {
            return 0;
        }

        int cur = addable ? root.val : 0;

        boolean curEven = (root.val & 1) == 0;

        return cur + process(root.left, curEven, fatherEven) + process(root.right, curEven, fatherEven);
    }

}
